Skip to content

drivers: counter: introduce counter capture api #89127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

XenuIsWatching
Copy link
Member

This introduces apis for input capture on the STM32. This is something
agnostic enough that is seen on other vendors such as Atmel's SAM line
and others.

A use case for this would be time stamping a rising and/or falling edge
where precision and lack of jitter is important. The only way to currently
do this in zephyr is to set up a GPIO interrupt and then read the counter
or kernel time... but this can have limitations of software such as the
processor may have a lot going on and can be subject to interrupt latencies.
Having a hardware 'latch' done of the rising and/or falling edge with
respect to a timer can eliminate these imprecisions.

How this is to be used, is that an application is to call
capture_callback_set with the configuration of the channel number, flags
(such as falling, rising, or both edges), and then a callback function. Then
the capture_enable function is to be called to enable this. The callback
has the timestamp of the edge in ticks and the edge that it captured (rising
or falling).

This also introduces an api for set the counter time.

Also, this set frequency to be 64 bits rather than 32 bits, as counters,
such as those that use fractional adders, tick at resolutions
greater than 4294967295Hz. This changes the freq in the common info
to a uint64_t. This retains the existing counter_get_freq function which
will still return a 32bit value. This also adds a counter_get_freq_64
function to get a 64bit value for the frequency.

Add inline helper functions for converting ticks to nanoseconds.

Also implement an inline helper for converting microseconds or
nanoseconds with a 64bit tick value.

Add a reset counter api to set the time back to 0.

Signed-off-by: Ryan McClelland <ryanmcclelland@meta.com>
@XenuIsWatching XenuIsWatching force-pushed the counter-cap branch 4 times, most recently from bed095d to eedf177 Compare April 26, 2025 16:19
Introduce a counter set value api to set the ticks for 32b and 64b.

Signed-off-by: Ryan McClelland <ryanmcclelland@meta.com>
This introduces apis for input capture on the STM32. This is something
agnostic enough that is seen on other vendors such as Atmel's SAM line
and others.

A use case for this would be time stamping a rising and/or falling edge
where precision and lack of jitter is important. The only way to currently
do this in zephyr is to set up a GPIO interrupt and then read the counter
or kernel time... but this can have limitations of software such as the
processor may have a lot going on and can be subject to interrupt
latencies. Having a hardware 'latch' done of the rising and/or falling
edge with respect to a timer can eliminate these imprecisions.

How this is to be used, is that an application is to call
`capture_callback_set` with the configuration of the channel number, flags
(such as falling, rising, or both edges), and then a callback function.
Then the `capture_enable` function is to be called to enable this. The
callback has the timestamp of the edge in ticks and the edge that it
captured (rising or falling).

Signed-off-by: Ryan McClelland <ryanmcclelland@meta.com>
Some counters, such as those that use fractional adders, tick at
resolutions greater than 4294967295Hz. This changes the freq in the common
info to a uint64_t. This retains the existing `counter_get_freq` function
which will still return a 32bit value. This also adds a
`counter_get_freq_64` function to get a 64bit value for the frequency.

Signed-off-by: Ryan McClelland <ryanmcclelland@meta.com>
Add inline helper functions for converting ticks to nanoseconds.

Also implement an inline helper for converting microseconds or
nanoseconds with a 64bit tick value.

Signed-off-by: Ryan McClelland <ryanmcclelland@meta.com>
@henrikbrixandersen
Copy link
Member

How is this functionally different from what is offered by the PWM capture API?

@henrikbrixandersen henrikbrixandersen added the Architecture Review Discussion in the Architecture WG required label Apr 26, 2025
@XenuIsWatching
Copy link
Member Author

How is this functionally different from what is offered by the PWM capture API?

That PWM capture measures the period and duty cycle of a pulse. This counter capture captures the exact time of a rising and/or falling edge when it happens.

@carlescufi
Copy link
Member

@XenuIsWatching when would it be a good time for you to present this in the Architecture WG meeting on Tuesdays?

@XenuIsWatching
Copy link
Member Author

@XenuIsWatching when would it be a good time for you to present this in the Architecture WG meeting on Tuesdays?

Hey @carlescufi , sorry for getting back to a bit late, I do plan on getting back to this, but right now I"m dealing with other priorities. I'll ping you went I can get around to making some slides

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Architecture Review Discussion in the Architecture WG required
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

4 participants